<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">

      <div class="title-container">
        <h1 class="title">欢迎进入系统</h1>
      </div>

      <el-form-item prop="userName">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="userName"
          v-model="loginForm.userName"
          placeholder="用户名"
          name="userName"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="passWord">
        <span class="svg-container">
          <svg-icon icon-class="passWord" />
        </span>
        <el-input
          :key="passWordType"
          ref="passWord"
          v-model="loginForm.passWord"
          :type="passWordType"
          placeholder="密码"
          name="passWord"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passWordType === 'passWord' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>

      <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">登 录</el-button>

    </el-form>
  </div>
</template>

<script>
import { validuserName } from '@/utils/validate'
import  userAPI   from '@/api/user'

export default {
  name: 'Login',
  data() {
    const validateuserName = (rule, value, callback) => {
      if (!validuserName(value)) {
        callback(new Error('请输入用户名'))
      } else {
        callback()
      }
    }
    const validatepassWord = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码不能少于六位'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        userName: '',
        passWord: ''
      },
      loginRules: {
        userName: [{ required: true, trigger: 'blur', validator: validateuserName }],
        passWord: [{ required: true, trigger: 'blur', validator: validatepassWord }]
      },
      loading: false,
      passWordType: 'passWord',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passWordType === 'passWord') {
        this.passWordType = ''
      } else {
        this.passWordType = 'passWord'
      }
      this.$nextTick(() => {
        this.$refs.passWord.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          //console.log(' submit 1：'+valid)
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm).then(() => {      //对应   src/store/moudles/user   user   login(
            this.$router.push({ path: this.redirect || '/' })
            this.loading = false
          }).catch((error) => {
            this.loading = false
            console.log(error +'submit!!+++Promise 被拒绝')
          })
        } else {
          console.log('error submit!!')
          return false
        }
       /*  if (valid) {
            this.loading = true;
            userAPI.login(this.loginForm).then(response => {
            if (response.code === 20000) {
                const { token } = response.data;
                console.log('Token:', token);
                console.log(this.loginForm);
                sessionStorage.setItem("token", token); // 将token存储到sessionStorage中
                this.$store.commit('user/setUser', user); // 假设你有一个方法来存储用户信息，比如Vuex的store
                this.$router.push({ path: this.redirect || '/' }); // 登录成功后跳转到相应页面
                } else {
                // 处理登录失败的情况
                console.error('Login failed:', response.msg);
              }
            this.loading = false; // 登录完成后，将loading设置为false
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        } */
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
     
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;
  background-image: url(../../assets/web.jpeg);      /*背景图片*/
  background-size: 100%;
  display: flex;
  align-items: center;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 35px 35px 0;
    margin: 0 auto;
    overflow: hidden;
   background-color: #283443;
   border-radius: 8px;
   opacity: 0.8;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
